package net.fukure.android.pecabc;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class Preference {

    SharedPreferences sp;
    MainActivity activity;
    
    public Preference(MainActivity activity) {
    	this.activity = activity;
    	this.sp = PreferenceManager.getDefaultSharedPreferences(activity);
	}

	void saveSetting(String host, String password, boolean remote, boolean tts) {
        Editor edit = sp.edit();
        edit.putString("PeercastHost", host);
        edit.putString("PeercastPassword", password);
        edit.putBoolean("RemoteServer", remote);
        edit.putBoolean("Tts", tts);
        edit.commit();
	}
	
	void saveBroadcastParam(JSONObject json){
        Editor edit = sp.edit();
        edit.putString("BroadcastParam", json.toString());
        edit.commit();
	}
	
	JSONObject loadBroadcastParam(){
		try {
			String jsonstr = sp.getString("BroadcastParam", null);
			return new JSONObject(jsonstr);
		} catch (JSONException e) {
			return new JSONObject();
		}
	}
	
	String loadPeercastPassword() {
        return sp.getString("PeercastPassword", "");
	}

	String loadPeercastHost() {
		String host = activity.resourceIDToString(R.string.peercast_host_default);
        return sp.getString("PeercastHost", host);
	}
	
	boolean loadUseRemote(){
        return sp.getBoolean("RemoteServer", false);
	}
	
	boolean loadUseTts(){
		return sp.getBoolean("Tts", false);
	}
	
	/*
	private String loadServerHost(){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        return sp.getString("ServerHost", resourceIDToString(R.string.server_host_default));
	}
	*/

	boolean loadBroadcastState(){
        return sp.getBoolean("Broadcasing", false);
	}

	void saveBroadcastState(boolean bool){
        sp.edit().putBoolean("Broadcasing", bool).commit();
	}
	
	void saveEncodeState(boolean bool){
        sp.edit().putBoolean("Encoding", bool).commit();
	}

	boolean loadEncodeState(){
        return sp.getBoolean("Encoding", false);
	}

	void saveOrientationPortrait(boolean portrait){
        sp.edit().putBoolean("Portrait", portrait).commit();
	}
	
	boolean loadOrientationPortrait(){
        return sp.getBoolean("Portrait", true);
	}
}
